package com.entities.projecttask.list;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;

import com.core.managers.SessionFactoryManager;
import com.core.protocols.ICustomDatasourceDelegate;
import com.core.protocols.IServiceLayerDelegate;
import com.core.protocols.ITableViewDatasource;
import com.core.protocols.ITableViewDelegate;
import com.core.ui.controller.base.CustomController;
import com.core.ui.customcell.TableViewCell;
import com.core.ui.customcell.TableViewDataCell;
import com.core.ui.customview.TableViewHeaderCell;
import com.core.utils.GlobalConstants;
import com.core.utils.HTMLTagUtils;
import com.core.utils.IndexPath;
import com.core.utils.LocalizableString;
import com.entities.projecttask.servicelayer.ProjectTasksListLayer;
import com.model.KeyValue;
import com.model.project.ProjectTask;

public class TaskTaskListController
        extends CustomController
        implements ITableViewDatasource, ITableViewDelegate, IServiceLayerDelegate {

	ArrayList <ProjectTask>           projectTasksList     = null;
	ProjectTasksListLayer             projectTaskListLayer = null;
	private String                    taskId               = null;
	private ICustomDatasourceDelegate delegate             = null;



	public String getTaskId () {

		return taskId;
	}



	public void setTaskId ( String value ) {

		this.taskId = value;
	}



	/**
	 * ICustomDatasource
	 * 
	 * 
	 */


	@Override
	public void setCustomDatasourceDelegate ( ICustomDatasourceDelegate delegate ) {

		this.delegate = delegate;

	}



	@Override
	public void reloadData () {

		projectTaskListLayer = new ProjectTasksListLayer();
		Integer result = null;
		try {
			result = Integer.parseInt(this.taskId);
		} catch ( NumberFormatException e ) {
			result = null;
		}


		projectTaskListLayer.objectByUNID(result, null, this);

	}



	/**
     *
     *
     *
     *
     */

	@Override
	public String tableStyleClassName () {

		return null;
	}



	@Override
	public String headerRowStyleClassNameAtIndex ( int index ) {

		return null;
	}



	@Override
	public String headerCellStyleClassNameAtIndexPath ( IndexPath indexPath ) {

		return null;
	}



	@Override
	public String cellStyleClassNameAtIndexPath ( IndexPath indexPath ) {

		return null;
	}



	@Override
	public String rowStyleClassNameAtIndex ( int index ) {

		return null;
	}



	@Override
	public int numberOfHeadRows () {

		return 1;
	}



	@Override
	public int numberOfHeadCols () {

		return 3;
	}



	@Override
	public TableViewHeaderCell headCellAtIndexPath ( IndexPath indexPath ) {

		LocalizableString localizableString = LocalizableString.sharedInstance();

		TableViewHeaderCell result = new TableViewHeaderCell();
		result.setStyleName("headerCellSimple");
		switch ( indexPath.getCol() ) {
			case 0: {
				result.setText("#");
				break;
			}
			case 1: {
				result.setText(localizableString.localizable("entity.ProjectTask.field.name"));
				break;
			}
			case 2: {
				result.setText(localizableString.localizable("grid.column.operation"));
				break;
			}

			default:
				break;
		}
		return result;
	}



	@Override
	public int numberOfDataRows () {

		if ( projectTasksList == null ) {
			return 0;
		} else {
			return projectTasksList.size();
		}
	}



	@Override
	public int numberOfDataCols () {

		return 3;
	}



	@Override
	public TableViewCell dataCellAtIndexPath ( IndexPath indexPath ) {

		LocalizableString localizableString = LocalizableString.sharedInstance();

		ProjectTask item = projectTasksList.get(indexPath.getRow());
		TableViewDataCell result = new TableViewDataCell();
		String idValue = item.idValue().toString();
		String idStrValue = item.idValue().getValue();


		switch ( indexPath.getCol() ) {
			case 0: {
				result.setStyleName("dataCellSimple");
				result.setText(String.format("%d", 1 + indexPath.getRow()));
				break;
			}
			case 1: {
				result.setStyleName("dataCellSimple maxwidth");
				result.setText(item.getName());
				result.setOnCellClick("%s?%s", GlobalConstants.SERVLET_TASKINFO, idValue);
				break;
			}
			case 2: {
				result.setStyleName("dataCellSimple");
				String link = String.format("%s?%s", GlobalConstants.SERVLET_TASKUPDATE, idValue);
				ArrayList <KeyValue> attrs = new ArrayList <KeyValue>();
				attrs.add(new KeyValue("href", link, true));
				attrs.add(new KeyValue("class", "trigger_lnk", true));
				String editText = HTMLTagUtils.customTag("a", attrs, localizableString.localizable("object.operation.edit"));

				String ajax = String.format("javascript:deleteTask(%s);", idStrValue);
				link = "javascript:void(0);";

				attrs = new ArrayList <KeyValue>();
				attrs.add(new KeyValue("href", link, true));
				attrs.add(new KeyValue("class", "trigger_lnk", true));
				attrs.add(new KeyValue("onclick", ajax, true));
				String deleteText = HTMLTagUtils.customTag("a", attrs, localizableString.localizable("object.operation.delete"));

				result.setText(String.format("%s|%s", editText, deleteText));
				break;

			}
			default: {
				break;
			}
		}
		return result;
	}



	@Override
	public <T> void responseResponseItemsList ( final List <T> items,
	                                            final Object request,
	                                            final Exception exception ) {

		projectTasksList = new ArrayList <ProjectTask>();

		if ( exception != null ) {
			return;
		}

		if ( items == null ) {
			return;
		}

		Session session = SessionFactoryManager.sharedInstance().openSession();
		try {

			ProjectTasksListLayer taskListLayer = new ProjectTasksListLayer();

			for ( T item : items ) {
				ProjectTask external = (ProjectTask) item;
				Integer id = external.getId();

				ProjectTask task = (ProjectTask) taskListLayer.objectByUNID(id, session, null);
				if ( task != null ) {
					for ( ProjectTask subtask : task.getSubTasks() ) {
						projectTasksList.add(subtask);
					}
				}
			}
		} finally {
			session.close();
		}
		if ( this.delegate != null ) {
			this.delegate.refreshContent();
		}

	}

}
